package peng.yang.express.VolleyUtils;

import android.content.Context;
import android.util.Log;
import android.widget.RelativeLayout;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import java.util.Map;

/**
 * Created by Administrator on 2016/6/13.
 */
public class RequestQueueUtils {

    private static RequestQueue requestQueue;


    public static RequestQueue getRequestQueue(Context context) {
        if (requestQueue == null) {
            requestQueue = Volley.newRequestQueue(context);
        }
        return requestQueue;
    }

    public static void exectueJsonRequest( Context context,String url, Response.Listener listener){
        getRequestQueue(context);
        JsonObjectRequest request = new JsonObjectRequest( url,null, listener, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.e("errorTag",volleyError.toString());
            }
        });
        requestQueue.add(request);
    }

    public static void exectueJsonRequest(Context context, String url, Response.Listener listener, Response.ErrorListener errorlistener){
        getRequestQueue(context);
        JsonObjectRequest request = new JsonObjectRequest( url,null, listener, errorlistener);
        requestQueue.add(request);
    }
    public static void exectueStringRequest(int method, Context context, String url, Response.Listener <String>listener, final Map<String, String> map){
        getRequestQueue(context);
        StringRequest request= new StringRequest(Request.Method.POST,url,listener, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.e("errorTag",volleyError.getMessage()+"");
            }
        }){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Log.e("errorTag",map.get("showapi_appid")+"");
                return map;
            }
        };
        requestQueue.add(request);


    }

}
